<?php
/**
 * 申请提现
 * 2022/.3.18
 * by_low
 */

class Withdrawal extends M_Controller
{

    /**
     *  用户申请提现操作
     */
    public function apply()
    {
        try {
            $field = $this->input->post();
            if ((int)$this->uid < 1) {
                throw new Exception('请登录', -4001);
            }
            if (empty($field['bank_id'])) {
                throw new Exception('请选择银行卡');
            }
            $captcha = $this->models('system/sms')->getCaptcha($field['phone'], 'withdrawal');
            if (empty($captcha)) {
                throw  new Exception('验证码不存在', 1);
            }
            if ($field['code'] <> $captcha) {
                throw new Exception('验证码错误');
            }
            $this->db->trans_begin();
            $bank = $this->models('bank')->cardInfo($field['bank_id']);
            if (empty($bank)) {
                throw new Exception('请正确选择银行卡');
            }
            if ($bank['uid'] <> $this->uid) {
                throw new Exception('请选择自己的银行卡');
            }
            $member = $this->models('member')->uidInfo(['uid' => $this->uid], 'uid,name,money');
            if ($member['money'] < $field['price']) {
                throw new Exception('请输入正确价格');
            }
            if ($field['price'] < 1) {
                throw new Exception('请输入正确金额');
            }
            $result = [
                'uid' => $member['uid'],
                'title' => $member['name'] . '申请' . $field['price'],
                'author' => $member['name'],
                'price' => $field['price'],
                'bank_name' => $bank['name'],
                'account' => $bank['number'],
                'bank' => $bank['bank_name'],
                'check' => 1,
                'inputtime' => time(),
                'inputip' => $_SERVER['REMOTE_ADDR'],
            ];
            $oid = $this->models('member/withdrawal')->addWithdrawal('apply', $result);
            if ($oid < 1) {
                throw new Exception('申请失败,请稍后重试', 1);
            }
            if (empty($this->models('member')->operationMemberField($member['uid'], 'money', -$result['price']))) {
                throw new Exception('添加失败, 请稍后重试', 1);
            }
            $this->db->trans_commit();
            apiSuccess(1, '申请成功');
        } catch (Exception $e) {
            if ($e->getCode() == 1) {
                $this->db->trans_rollback();
            }
            apiSuccess($e->getCode(), $e->getMessage());
        }
    }


    // 申请提现列表
    public function applyList()
    {
        if ((int)$this->uid < 1) {
            apiSuccess(-4001, '请登录');
        }
        $page = max($_GET['page'], 1);
        $pageSize = (int)$_GET['paeSize'] ?: 10;
        $code = $this->input->get('code');
        $status = $this->input->get('status');
        if (!in_array($code, ['apply', 'store_apply'])) {
            apiSuccess(0, '请求参数错误');
        }
        $result = $this->models('member/withdrawal')->applyList(
            $code, array_filter(['uid' => $this->uid, 'status' => $status]), $page, $pageSize
        );
        apiSuccess(1, 'success', [
            'list' => array_map(function (&$item) {
                return $this->models('member/withdrawal')->transferApply($item);
            }, $result),
            'end' => count($result) < $pageSize
        ]);
    }

    /**
     * 用户收支记录
     * 包含 : 购买藏品
     * 购买盲盒
     * 充值
     * 申请提现
     */
    public function memberPayLog()
    {
        if ((int)$this->uid < 1) {
            apiSuccess(-4001, '请登录');
        }
        $page = max($_GET['page'], 1);
        $pageSize = (int)$_GET['pageSize'] ?: 10;
        $result = $this->models('member/withdrawal')->memberPayLog($this->uid, $page, $pageSize);
        apiSuccess(1, 'success', [
            'list' => array_map(function (&$item) {
                return $this->models('member/withdrawal')->payLogTransfer($item);
            }, $result),
            'end' => $pageSize > count($result)
        ]);
    }

}